package com.crmweb.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.crmweb.dto.XTreeDto;
import com.crmweb.entity.Modules;
import com.crmweb.dao.RolesDao;
import com.crmweb.entity.Roles;
import com.crmweb.service.RolesService;
@Service
public class RolesServiceImpl implements RolesService{
	@Resource
	private RolesDao rolesDao;
	@Override
	public int saveRole(String name) {
		// TODO Auto-generated method stub
		return rolesDao.saveRole(name);
	}


	@Override
	public int updateRole(Roles roles) {
		// TODO Auto-generated method stub
		return rolesDao.updateRole(roles);
	}

	@Override
	public int deleteRole(String rolesId) {
		// TODO Auto-generated method stub
		return rolesDao.deleteRole(rolesId);
	}

	@Override
	public Roles getRoleByName(String rolesName) {
		// TODO Auto-generated method stub
		return rolesDao.getRoleByName(rolesName);
	}

	@Override
	public List<Roles> getRolesByUserId(String usersId) {
		// TODO Auto-generated method stub
		return rolesDao.getRolesByUserId(usersId);
	}

	@Override
	public Roles getRoleByRoleId(String rolesId) {
		// TODO Auto-generated method stub
		return rolesDao.getRoleByRoleId(rolesId);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return rolesDao.getCount();
	}

	@Override
	public List<Roles> getAllRoles() {
		// TODO Auto-generated method stub
		return rolesDao.getAllRoles();
	}

	@Override
	public List<Roles> getRolesByCondition(Roles role) {
		// TODO Auto-generated method stub
		return rolesDao.getRolesByCondition(role);
	}
	
	@Override
	public List<Modules> selAllModules() {
		// TODO Auto-generated method stub
		return rolesDao.selAllModules();
	}
	
	
	@Override
	public List<Modules> ModuleTree(List<Modules> list) {
		// TODO Auto-generated method stub
		List<Modules> plist=new ArrayList<Modules>();
		for (Modules modules : list) {
			if(modules.getParentId()==0) {
				plist.add(modules);
			}
		}
		for (Modules ctree : plist) {
			ctree.setChildren(getChildren(ctree.getId(), list));
		}
		return plist;
		
	}
	
	public List<Modules> getChildren(int pid,List<Modules> nodes){
		List<Modules> clist=new ArrayList<Modules>();
		for (Modules cmodules : nodes) {
			if(cmodules.getParentId()==pid) {
				clist.add(cmodules);
			}
		}
		for (Modules ccmodules : clist) {
			ccmodules.setChildren(getChildren(ccmodules.getId(), nodes));
		}
		return clist;
	}



	@Override
	public List<XTreeDto> TirmXTreeDto(String id) {
		// TODO Auto-generated method stub
		List<XTreeDto> xlist=new ArrayList<XTreeDto>();
		List<Modules> list=selAllModules();
		List<Integer> mlist=selModulesIdByRid(id);
		
		
		for (Modules modules : list) {
			XTreeDto x=new XTreeDto();
			x.setId(modules.getId());
			x.setPid(modules.getParentId());
			x.setTitle(modules.getName());
		    x.setValue(String.valueOf(modules.getId()));
		    if(mlist.contains(modules.getId())) {
				x.setChecked(true);
				
			}else {
				x.setChecked(false);
				
			}
		    
			xlist.add(x);

		}
		return xlist;
	}

	public List<XTreeDto> xTreeDtoTree(List<XTreeDto> list) {
		// TODO Auto-generated method stub
		List<XTreeDto> plist=new ArrayList<XTreeDto>();
		for (XTreeDto xTreeDto : list) {
			if(xTreeDto.getPid()==0) {
				plist.add(xTreeDto);
			}
			}
		for (XTreeDto p1 : plist) {
			p1.setData(getChil(p1.getId(), list));
		}
		return plist;
	}

	
	public List<XTreeDto> getChil(int pid,List<XTreeDto> nodes){
		List<XTreeDto> plist=new ArrayList<XTreeDto>();
		for (XTreeDto c : nodes) {
			if(c.getPid()==pid) {
				plist.add(c);
			}
		}
		for (XTreeDto cc : plist) {
			cc.setData(getChil(cc.getId(), nodes));
		}
		return plist;
	}

	@Override
	public List<Integer> selModulesIdByRid(String rid) {
		// TODO Auto-generated method stub
		return rolesDao.selModulesIdByRid(rid);
	}

	@Override 
	public int addModulesByRolesId(String rid,List<Integer> mid) {
		// TODO Auto-generated method stub
		return rolesDao.addModulesByRolesId(rid, mid);
	}


	@Override
	public int delModulesByrolesId(String roleid) {
		// TODO Auto-generated method stub
		return rolesDao.delModulesByrolesId(roleid);
	}

}
